package com.nsl.my_youlai.fast2json_demo;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.reader.ObjectReaderProvider;
import com.alibaba.fastjson2.writer.ObjectWriterProvider;
import com.nsl.my_youlai.mapper.DateTime1Mapper;
import com.nsl.my_youlai.mapper.DocxTemplateMapper;
import com.nsl.my_youlai.model.entity.DocxTemplateEntity;
import com.nsl.my_youlai.model.entity.test.DatetimeEntity1;
import enums.learn.ColorEnum;
import enums.learn.ColorEnumReader;

import enums.learn.UseColorEnumBo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@SpringBootTest
public class Demo1 {

    @Autowired
    private DocxTemplateMapper docxTemplateMapper;

    @Autowired
    private DateTime1Mapper dateTime1Mapper;

    @Test
    public void normalTest(){
        List<DocxTemplateEntity> docxTemplateEntityList=docxTemplateMapper.selectList(null);

        DocxTemplateEntity docxTemplateEntity=docxTemplateEntityList.get(0);
        System.out.println(JSON.toJSONString(docxTemplateEntity));
        System.out.println(docxTemplateEntity);
        System.out.println(docxTemplateEntity);
        System.out.println(LocalDateTime.now());

        List<DatetimeEntity1> datetimeEntity1List=dateTime1Mapper.selectList(null);
        System.out.println(datetimeEntity1List.get(0));

        DatetimeEntity1 datetimeEntity2=new DatetimeEntity1();
        datetimeEntity2.setDateTimeA(LocalDateTime.now());
        datetimeEntity2.setDateTimeJavaString("2000-01-01");

        DatetimeEntity1 datetimeEntity1A=new DatetimeEntity1();
        datetimeEntity1A.setDateTimeA(LocalDateTime.now());
        //datetimeEntity1A.setDateTimeJavaString("20000102");
        //datetimeEntity1A.setDateTimeJavaString("20102");
        //datetimeEntity1A.setDateTimeJavaString("102");
        dateTime1Mapper.insert(datetimeEntity1A);
    }

    //reader writer 测试ok
    @Test
    public void ColorTest2(){
        //全局生效
        JSON.register(ColorEnum.class,new ColorEnumWriter());

        UseColorEnumBo useColorEnumBo=new UseColorEnumBo();
        useColorEnumBo.setColorEnum(ColorEnum.YELLOW);
        useColorEnumBo.setValue(10L);

        //#region
        //局部生效的反序列化
        ObjectWriterProvider writerProvider=new ObjectWriterProvider();
        writerProvider.register(ColorEnum.class,new ColorEnumWriter());
        JSONWriter.Context contextWriter= JSONFactory.createWriteContext(writerProvider);
        //String jsonResult=JSON.toJSONString(useColorEnumBo,contextWriter);
        //#endregion

        String jsonResult=JSON.toJSONString(useColorEnumBo);
        log.info("jsonResult = {}",jsonResult);

        System.out.println(JSON.toJSONString(useColorEnumBo));

        //局部生效的反序列化
        ObjectReaderProvider objectReaderProvider=new ObjectReaderProvider();
        objectReaderProvider.register(Long.class,new MyBigDecimalReader());
        objectReaderProvider.register(ColorEnum.class,new ColorEnumReader());

        String jsonString="{\"colorEnum\":{\"label\":\"黄\",\"value\":1},\"value\":10}";

        JSONReader.Context context=new JSONReader.Context(objectReaderProvider);
        UseColorEnumBo useColorEnumBo1=JSON.parseObject(jsonString,UseColorEnumBo.class,context);

        log.info("useColorEnumBo1 = {}",useColorEnumBo1);
        log.info("useColorEnumBo1.toJson = {}",JSON.toJSONString(useColorEnumBo1));
    }

    //writer 写法更简洁
    @Test
    public void test3(){
        String jsonString = "{\"data\":\"1.111\"}";

        ObjectReaderProvider provider=new ObjectReaderProvider();
        provider.register(BigDecimal.class, MyBigDecimalReader.INSTANCE);

        UseBigDecimalPojo useBigDecimalPojo1 =JSON.parseObject(jsonString, UseBigDecimalPojo.class,JSONFactory.createReadContext(provider));
        //LongUsePojo longUsePojo=JSON.parseObject(jsonString,LongUsePojo.class);
        System.out.println(useBigDecimalPojo1);


    }
}
